package com.ilancuo.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public class TestPerson {
    public static void main(String[] args) {
//        newInstance();
//        newInstance1();
//        testFields();
//        testMethods();
        testMethods1();
    }

    private static void newInstance() {
        Class<Person> personClass = Person.class;
        try {
            Constructor<Person> constructor = personClass.getConstructor();
            Person person = constructor.newInstance();
            System.out.println(person);
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private static void newInstance1() {
        Class<Person> personClass = Person.class;
        try {
            Constructor constructor = personClass.getConstructor(String.class, String.class);
            Person person = (Person) constructor.newInstance("ilancuo", "Qingdao");
            System.out.println(person);
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private static void testFields() {
        Class<Person> personClass = Person.class;
        Field[] fields = personClass.getDeclaredFields();
        System.out.println(Arrays.toString(fields));
    }

    private static void testMethods() {
        Class<Person> personClass = Person.class;
        Method[] methods = personClass.getMethods(); //包括继承的方法
        System.out.println(Arrays.toString(methods));
    }

    private static void testMethods1() {
        Class<Person> personClass = Person.class;
        Method[] declaredMethods = personClass.getDeclaredMethods(); //不包括继承的方法
        System.out.println(Arrays.toString(declaredMethods));
    }
}
